/**
 * Created by xiebang
 * Description
 * User:小谢
 * Date:2021-09-19
 * Time:22:44
 */

class ListNodeD {
    public int val;
    public ListNodeD prev;
    public ListNodeD next;

    public ListNodeD(int val){

        this.val = val;
        prev = null;
        next = null;
    }
}
public class MyRealLinkedList {
    public ListNodeD head ;//头
    public ListNodeD last;//尾
    public MyRealLinkedList(){
    this.head = new ListNodeD(-1);
    }
    //头插法
    public void addFist(int val){
        ListNodeD newnode = new ListNodeD(val);
        if (this.head == null){
            this.head = newnode;
        }else {
            newnode.next = this.head;
            this.head.prev = newnode;
            this.head = newnode;
        }
    }
    //尾插法
    public void addLast(int val){
        ListNodeD newnode = new ListNodeD(val);
        if (this.head == null){
            this.head = newnode;
            this.last = newnode;
        }else {
            this.last.next = newnode;
            newnode.prev = this.last;
            this.last = newnode;
        }
    }

    //任意位置插入，第一个数据节点为0号下标
    public void addIndex(int index,int val) {
        if (index<0 || index > size1()){
            System.out.println("坐标非法");
            return;
        }
        if (index == 0){
            this.addFist(val);
            return;
        }

        if (index == size1()){
            this.addLast(val);
            return;
        }
        ListNodeD newnode = new ListNodeD(val);
        ListNodeD cur = this.head;

        while (index != 0){
            cur = cur.next;
            index--;
        }
        newnode.next = cur;
        newnode.prev = cur.prev;
        cur.prev.next = newnode;
        cur.prev = newnode;
    }
    //查找是否包含关键字key是否在单链表中
    public boolean conatains(int key){
        if (this.head == null){
            return false;
        }
        ListNodeD cur = this.head;
        while (cur != null){
            if (cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //得到单链表的长度
    public int size1(){
        int len = 0;
        if (this.head == null) {
            return len;
        }
       ListNodeD cur = this.head;
       while (cur!=null){
           len++;
           cur = cur.next;
       }
        return len;
    }

    public int size2(){
        int len = 0;
        if (this.head == null) {
            return len;
        }
        ListNodeD cur = this.last;
        while (cur!=null){
            len++;
            cur = cur.prev;
        }
        return len;
    }

    //打印链表
    public void display(){
        if (this.head.next == null) {
            return;
        }
        ListNodeD cur = this.head;
        while (cur != null){
            System.out.print(cur.val + " -> ");
            cur = cur.next;
        }
        System.out.println("null");
    }
//删除第一次出现的val
    public void remove(int val){
        if (this.head== null){
            return;
        }


        ListNodeD cur = this.head;
        while (cur != null){
            if (cur.val == val){
                break;
            }
            cur = cur.next;
        }

        if (cur.val == val){
            if (cur == this.head){
                this.head = this.head.next;
                if (this.head == null){
                    this.last = null;
                    return;
                }else {
                    this.head.prev = null;
                    return;
                }
            }

            if (cur == this.last){
                this.last = this.last.prev;
                this.last.next = null;
                return;
            }

            cur.prev.next = cur.next;
            cur.next.prev = cur.prev;
        }
    }

    public void  removeAllVal(int val){
        ListNodeD cur = this.head;
        while (cur != null){
            if (cur.val == val){
                //判断是否为头结点
                if (cur == this.head){
                    this.head = this.head.next;
                    if (this.head == null){
                        this.last = null;
                    }else {
                        this.head.prev = null;
                    }
                } else {
                    cur.prev.next = cur.next;
                    if (cur.next == null) {
                        this.last = cur.prev;
                    } else {
                        cur.next.prev = cur.prev;
                    }
                }
                cur = cur.next;
            }else {
                cur = cur.next;
            }
        }
    }
    public void clean(){
        if (this.head == null){
            return;
        }
        ListNodeD cur = this.head;
        while (cur != null){
            ListNodeD curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        this.head = null;
        this.last = null;
    }
}

